package com.studentstreet;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.studentstreet.tools.PreferenceTools;

public class MainActivity extends TabActivity {
	// tabHost
	public TabHost mTabHost;
	// RadioGroup,即页面下方的各个选项的集合
	public RadioGroup mRadioGroup;
	// RadioButton
	private RadioButton rb0, rb1, rb2, rb3, rb4;

	// Title
	private TextView titleText;

	// Login Button
	private Button login_bt;
	
	//OEM++ 2013-03-22
	//logoButton
	private ImageButton logoButton = null;
	//OEM--
	
	// 各个标签
	public static final String TAB_HOME = "HOME";
	public static final String TAB_ZKW = "ZKW";
	public static final String TAB_SC = "SC";
	public static final String TAB_GR = "GR";
	public static final String TAB_GD = "GD";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		// 将当前Activity压入堆栈中
		MyApp.getInstance().addActivity(this);
		
		//OEM++  初始化logoImageButton
		logoButton = (ImageButton)findViewById(R.id.header_imgBt);
		logoButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(),AboutUsActivity.class);
				startActivity(intent);
			}
		});
		//OEM--
		
		// 初始化TitleTextView
		titleText = (TextView) findViewById(R.id.title);

		// 初始化TabHost
		mTabHost = this.getTabHost();

		// 初始化RadioButton
		initRadioButton();

		// 初始化RadioGroup
		mRadioGroup = (RadioGroup) findViewById(R.id.main_radio);

		// 初始化登录按钮
		login_bt = (Button) findViewById(R.id.login_bt);
		login_bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 显示登录对话框
				Intent intent = new Intent(MainActivity.this, LoginDialog.class);
				startActivity(intent);
			}
		});

		TabSpec mTabSpec1 = mTabHost.newTabSpec(TAB_HOME)
				.setIndicator(TAB_HOME);
		mTabSpec1.setContent(new Intent(this, SellerActivity.class));
		mTabHost.addTab(mTabSpec1);

		TabSpec mTabSpec2 = mTabHost.newTabSpec(TAB_ZKW).setIndicator(TAB_ZKW);
		mTabSpec2.setContent(new Intent(this, GoodsActivity.class));
		mTabHost.addTab(mTabSpec2);

		TabSpec mTabSpec3 = mTabHost.newTabSpec(TAB_SC).setIndicator(TAB_SC);
		mTabSpec3.setContent(new Intent(this, FavoritesActivity.class));
		mTabHost.addTab(mTabSpec3);

		TabSpec mTabSpec4 = mTabHost.newTabSpec(TAB_GR).setIndicator(TAB_GR);
		mTabSpec4.setContent(new Intent(this, PersonalActivity.class));
		mTabHost.addTab(mTabSpec4);

		TabSpec mTabSpec5 = mTabHost.newTabSpec(TAB_GD).setIndicator(TAB_GD);
		mTabSpec5.setContent(new Intent(this, MoreActivity.class));
		mTabHost.addTab(mTabSpec5);

		mTabHost.setCurrentTabByTag(TAB_HOME);
		// 设置RadioGroup选项改变的监听事件
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// System.out.println(checkedId + "---------");
				setRadioText(checkedId);
				switch (checkedId) {
				case R.id.radio_button0:
					titleText.setText(getResources().getString(
							R.string.dianpujie));
					mTabHost.setCurrentTabByTag(TAB_HOME);
					break;
				case R.id.radio_button1:
					titleText.setText(getResources().getString(
							R.string.zhongkouwei));
					mTabHost.setCurrentTabByTag(TAB_ZKW);
					break;
				case R.id.radio_button2:
					titleText.setText(getResources().getString(
							R.string.shoucang));
					mTabHost.setCurrentTabByTag(TAB_SC);
					break;
				case R.id.radio_button3:
					titleText.setText(getResources().getString(R.string.geren));
					mTabHost.setCurrentTabByTag(TAB_GR);
					break;
				case R.id.radio_button4:
					titleText.setText(getResources()
							.getString(R.string.gengduo));
					mTabHost.setCurrentTabByTag(TAB_GD);
					break;
				}
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		PreferenceTools prefTools = new PreferenceTools(this);
		// 判断是否已经登录
		if (!prefTools.isLogin()) {
			return;
		}
		// 登录后修改登录按钮上的text
		String userName = prefTools.getUserName();
		if (!userName.equals("")) {
			login_bt.setText(userName);
			// 去掉登录按钮文本右边的图标
			login_bt.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					null);
		}
	}

	// 初始化RadioButton
	private void initRadioButton() {
		rb0 = new RadioButton(this);
		rb1 = new RadioButton(this);
		rb2 = new RadioButton(this);
		rb3 = new RadioButton(this);
		rb4 = new RadioButton(this);
		rb0 = (RadioButton) findViewById(R.id.radio_button0);
		rb1 = (RadioButton) findViewById(R.id.radio_button1);
		rb2 = (RadioButton) findViewById(R.id.radio_button2);
		rb3 = (RadioButton) findViewById(R.id.radio_button3);
		rb4 = (RadioButton) findViewById(R.id.radio_button4);
	}

	// 设置RadioButton的字体颜色
	public void setRadioText(int checkedId) {
		switch (checkedId) {
		case R.id.radio_button0:
			// System.out.println("radioButton" + R.id.radio_button0);
			mTabHost.setCurrentTabByTag(TAB_HOME);
			rb0.setTextColor(Color.RED);
			rb1.setTextColor(Color.WHITE);
			rb2.setTextColor(Color.WHITE);
			rb3.setTextColor(Color.WHITE);
			rb4.setTextColor(Color.WHITE);
			break;
		case R.id.radio_button1:
			// System.out.println("radioButton" + R.id.radio_button1);
			rb1.setTextColor(Color.RED);
			rb0.setTextColor(Color.WHITE);
			rb2.setTextColor(Color.WHITE);
			rb3.setTextColor(Color.WHITE);
			rb4.setTextColor(Color.WHITE);
			break;
		case R.id.radio_button2:
			// System.out.println("radioButton" + R.id.radio_button2);
			rb2.setTextColor(Color.RED);
			rb1.setTextColor(Color.WHITE);
			rb0.setTextColor(Color.WHITE);
			rb3.setTextColor(Color.WHITE);
			rb4.setTextColor(Color.WHITE);
			break;
		case R.id.radio_button3:
			// System.out.println("radioButton" + R.id.radio_button3);
			rb3.setTextColor(Color.RED);
			rb1.setTextColor(Color.WHITE);
			rb2.setTextColor(Color.WHITE);
			rb0.setTextColor(Color.WHITE);
			rb4.setTextColor(Color.WHITE);
			break;
		case R.id.radio_button4:
			// System.out.println("radioButton" + R.id.radio_button4);
			rb4.setTextColor(Color.RED);
			rb1.setTextColor(Color.WHITE);
			rb2.setTextColor(Color.WHITE);
			rb3.setTextColor(Color.WHITE);
			rb0.setTextColor(Color.WHITE);
			break;
		}
	}

}
